package com.chy.blog_server.modular.map.controller;

import com.chy.blog_server.core.util.PageAndParamUtil;
import com.chy.blog_server.core.util.StringUtil;
import com.chy.blog_server.modular.map.entity.MapDistrict;
import com.chy.blog_server.modular.map.service.IMapDistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：行政地区 开放端controller
 * 作者：chenhuiyan
 * 时间：2025/03/22 22:07
 */
@Controller
@RequestMapping(value = "/map/publicMapDistrict")
public class PublicMapDistrictController {

    @Autowired
    private IMapDistrictService mapDistrictService;

    /**
     * 查询省份列表
     * @return
     */
    @RequestMapping(value = "/provinceList")
    @ResponseBody
    public Map<String, Object> provinceList(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            List<MapDistrict> list = mapDistrictService.provinceList(); //获取数据列表
            res.put("code", 0);
            res.put("msg", "获取数据成功");
            res.put("count", list.size());
            res.put("data", list);
        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

    /**
     * 查询城市列表：并且级别为城市
     * @return
     */
    @RequestMapping(value = "/cityList")
    @ResponseBody
    public Map<String, Object> cityList(HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        try{
            Map<String, Object> params = PageAndParamUtil.getParams();
            String provinceCode = params.get("provinceCode") + "";
            if(StringUtil.isNull(provinceCode)){
                res.put("code", 500);
                res.put("msg", "省份为空值，无法初始化城市");
            }else{
                List<MapDistrict> list = mapDistrictService.cityList(provinceCode); //获取数据列表
                res.put("code", 0);
                res.put("msg", "获取数据成功");
                res.put("count", list.size());
                res.put("data", list);
            }

        }catch (Exception e){
            e.printStackTrace();
            res.put("code", 500);
            res.put("msg", "获取数据失败");
        }
        return res;
    }

}
